package com.ulic.customerManApplication.util.excel;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Vector;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
 * 导出数据到excel表
 * @author Administrator
 *
 */
public class ExportExecl {
	//创建一个工作簿
	public static HSSFWorkbook wb = null;   
	//创建一个sheet
    public static HSSFSheet sheet = null;
	/**
	 * @param excleName  导出的excel名字
	 * @param list       导出对象的所有excel数据 其中
	 * @param n          导出对象的列数
	 * @param str[]      第一行数据
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public  boolean exportToExcel(String excleName,@SuppressWarnings("rawtypes") Map vec,String str[]){
		boolean sucess = false;
		//判断vec是否为空
		if(vec == null || vec.size() == 0){
			return sucess;
		}
		//创建一个工作簿
		wb = new HSSFWorkbook();   
		//创建一个sheet
	    sheet = wb.createSheet();
	    //设置表头和第一行数据
	    setSheetHeader(str);
	    //将vec中的每一个对象的所有属性放入List中
	    for(int row=0;row<vec.size();row++){
	    	//创建行以及单元格
	    	List<String> cells = (List<String>)vec.get(row);
		    setRow((short)(row+1), cells);
	    }
	    try {
			//得到导出的路径
			String path = getPath(excleName);
			FileOutputStream fileOut = new FileOutputStream(path);    
		    wb.write(fileOut);    
		    fileOut.close();  
		    sucess = true;
		    return sucess;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return sucess;
		} catch (IOException e) {
			e.printStackTrace();
			return sucess;
		}
	}

	public static String getPath(String excleName) throws FileNotFoundException {
		ResourceBundle boundle = ResourceBundle.getBundle("parameter");
		String basePath = boundle.getString("exportExclePath");
		String path = basePath+excleName;
		return path;
	}
	
	/**
	 * 创建表头信息
	 * @param sheet
	 * @param title 标题
	 * @param str   第一行数据
	 */
	private void setSheetHeader(String str[]){
	    HSSFRow first_row = sheet.createRow(0);
	    for(int k=0;k<str.length;k++){
	    	HSSFCell chell = first_row.createCell((short)k);
	    	chell.setEncoding(HSSFCell.ENCODING_UTF_16);
	    	chell.setCellValue(str[k]);
	    }
	}
	
	
	
	/**
	 * 创建行以及单元格
	 * @param rows
	 * @param cells
	 */
	private void setRow(short rows,List<String> cells){
		HSSFRow row = sheet.createRow(rows);
		HSSFCell cell = null;
		for(int i=0;i<cells.size();i++){
			cell = row.createCell((short)i);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue(cells.get(i));
		}
	}
	
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void main(String args[]) throws FileNotFoundException, IOException{
		Vector vec = new Vector();
		@SuppressWarnings("unused")
		ExportExecl exportExecl = new ExportExecl();
		List list = new ArrayList();
		list.add("6090118000511000035");
		list.add("6090118000511000036");
		vec.add(list);
	}
}
